package com.hongxun.budget.ui.dialog;

import android.text.InputType;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.utils.KeyBoardUtils;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.interfaces.ResponseListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/7/25.
 */

public class InputDialog extends BaseCenterFragmentDialog {

    @BindView(R.id.title)
    TextView titleTv;
    @BindView(R.id.content)
    EditText content;
    String title;
    int inputType = -999;
    ResponseListener listener;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_input;
    }

    @Override
    protected void initViews() {
        if (!TextUtils.isEmpty(title)) titleTv.setText(title);
        if (inputType != -999) {
            if (inputType == InputType.TYPE_NUMBER_FLAG_DECIMAL) {
                content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            } else
                content.setInputType(inputType);
        }
        KeyBoardUtils.openKeybord(content, getActivity());
    }

    public InputDialog setTitle(String title) {
        this.title = title;
        if (titleTv != null) titleTv.setText(title);
        return this;
    }

    public InputDialog setInputType(int inputType) {
        this.inputType = inputType;
        if (content != null) {
            if (inputType == InputType.TYPE_NUMBER_FLAG_DECIMAL) {
                content.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            } else
                content.setInputType(inputType);
        }
        return this;
    }


    public InputDialog setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }


    @OnClick(R.id.confirm)
    public void onViewClicked() {
        String result = content.getText().toString();
        if (!TestingUtils.isEmpty(context, result, "您的输入数据")) return;
        listener.response(result);
        KeyBoardUtils.closeKeybord(content, getActivity());
        dismiss();
    }
}
